<template>
  <div>
    <basic-container>
      <avue-tabs :option="option"
                 v-model="form"
                 ref="ruleForm"
                 @change="handleChange"
                 @submit="handleSubmit"></avue-tabs>
    </basic-container>
  </div>
</template>

<script>
import option from "@/const/user/info";
import { getUserInfo,restOwnPassword,updateOwnInfo} from '@/api/user'
import { SUCCESS } from '@/config/env';
export default {
  data() {
    return {
      type: "info",
      option: option(this),
      form: {},
      updateInfo:{
        phone: '',
      },
      restPasswordFrom:{
        oldPassword: '',
        newPassword: ''
      }
    };
  },
  created() {
    this.handleWitch();
  },
  methods: {
    handleSubmit() {
      if(this.type === "info"){
       this.updateInfo = {
         phone: this.form.phone
       }
        updateOwnInfo(this.updateInfo).then((response =>{
          if(response.data.code === SUCCESS){
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          }else{
            this.$notify({
              title: '失败',
              message: response.data.msg,
              type: 'error',
              duration: 2000
            })
          }
        }))
      }else if(this.type === "password"){
        this.restPasswordFrom={
          oldPassword: this.form.oldpassword,
          newPassword: this.form.newpassword
        }

        restOwnPassword(this.restPasswordFrom).then((response) =>{
          if(response.data.code === SUCCESS){
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.$store.dispatch('LogOut').then(() => {
              location.reload()
            })
          }else{
            this.$notify({
              title: '失败',
              message: response.data.msg,
              type: 'error',
              duration: 2000
            })
          }
        })
      }

    },
    handleWitch() {
      if (this.type === "info") {
        getUserInfo().then((res) => {
          const data = res.data.data;
          this.form = data.sysUser
        })
      } else if (this.type === "password") {
        this.form = {

        };
      }
    },
    handleChange(item) {
      this.type = item.prop;
      this.handleWitch();
    }
  }
};
</script>

<style>
</style>
